﻿<?PHP
	//ini_set('display_errors', 1);
	
	require 'config.php';	
	require 'CyrillicEncoder.php';	
	require 'checkSession.php';
	
	$vLink = mysql_connect($DBServer, $DBUser, $DBPassword);
	if(!$vLink) 
		die(mysql_error());
	//setting session encoding
	mysql_select_db($DBName, $vLink);	
	mysql_query("SET NAMES 'cp1251';", $vLink);
	mysql_query("SET CHARACTER SET 'cp1251';", $vLink);
	mysql_query("SET SESSION collation_connection = 'cp1251_general_ci';", $vLink);
	
	//getting UID
	$vUID = GetObjectID();	
	//$vUID = getUserIDByKey();
	//generating XML
	$XMLResult = '';
	$XMLResult .= "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n";
	$XMLResult .= "<root>" . "\n";
	if($vUID  > -1)
	{
		$XMLResult .= getSettingsXML();
		$XMLResult .= getRooms($vUID);
		$XMLResult .= getImages($vUID, false);
		$XMLResult .= getRoomsImages($vUID);		
	}
	else
	{		
		$XMLResult .= getErrorXML();
	}
	$XMLResult .= "</root>" . "\n";
	echo $XMLResult;
	
	function GetObjectID()
	{
		$vResult = -1;
		if(isset($_GET['obj_id']))
			$vResult = intval($_GET['obj_id']);		
		return $vResult;
	}
	
	function getRooms($UserID)
	{
		//getting numbers attached to object	
		$vQuery = "SELECT id, name, total, rooms, vnomere, about FROM dikar2011_nomer WHERE owner=$UserID";
		$vResults = mysql_query($vQuery);			
		$vResult = "<rooms>\n";
		while($vLine = mysql_fetch_assoc($vResults)) 
		{
			$vID = formatXML('id', $vLine['id']);
			$vName = formatXML('name', cp1251_to_utf8($vLine['name']));
			$vRooms = formatXML('total', $vLine['total']);
			$vApartments = formatXML('rooms', $vLine['rooms']);
			$vInRoom = formatXML('vnomere', cp1251_to_utf8($vLine['vnomere']));
			$vAbout = formatXML('about', cp1251_to_utf8($vLine['about']));			
			$vResult = $vResult . "<room" . $vID . $vName . $vRooms . $vApartments . $vInRoom . $vAbout . "/>\n";
		}
		$vResult = $vResult . "</rooms>";
		return $vResult;
	}
	
	function getImages($UserID, $IsRoom)
	{	
		if($IsRoom)
			$vQuery = "SELECT id, description, ord, to_del, public FROM dikar2011_photos WHERE id_obj=$UserID AND owner=9 ORDER BY ord";
		else
			$vQuery = "SELECT id, description, ord, to_del, public FROM dikar2011_photos WHERE id_obj=$UserID AND owner=1 ORDER BY ord";	
		$vResults = mysql_query($vQuery);		
		$vResult = "<images" . formatXML('object_id', $UserID) . ">\n";
		while($vLine = mysql_fetch_assoc($vResults)) 
		{
			$vID = formatXML('id', $vLine['id']);			
			$vDescription = formatXML('description', cp1251_to_utf8($vLine['description']));
			$vOrder = formatXML('ord', $vLine['ord']);
			$vToDelete = formatXML('to_del', $vLine['to_del']);
			$vIsPublic = formatXML('published', $vLine['public']);
			$vResult = $vResult . "<image" . $vID . $vDescription . $vOrder . $vToDelete . $vIsPublic . "/>\n";
		}
		$vResult = $vResult . "</images>";
		return $vResult;
	}
	
	function getRoomsImages($UserID)
	{
		$vRooms = getRoomsList($UserID);		
		$vResult = '';
		foreach($vRooms as &$vRoom)
		{
			$vResult = $vResult . getImages($vRoom[0], true);
		}
		return $vResult;
	}
	
	function getRoomsList($UserID)
	{
		$vQuery = "SELECT id, name FROM dikar2011_nomer WHERE owner=$UserID";
		$vResults = mysql_query($vQuery);
		$vResult = array();
		while($vLine = mysql_fetch_assoc($vResults))
		{
			$vID = $vLine['id'];
			$vName = cp1251_to_utf8($vLine['name']);
			$vResult[] = array($vID, $vName);
		}
		return $vResult;
	}
	
	function getSettingsXML()
	{
		global $sizeWatermarked;
		$vResult = "<settings>";
		$vResult .= formatXMLSetting('Size', $sizeWatermarked);
		$vResult .= "</settings>";
		return $vResult;
	}
	
	function formatXMLSetting($Name, $Raw)
	{
		$vResult = "<" . $Name . formatXML('value', $Raw) . "/>\n";				
		return $vResult;
	}
	
	//closing session	
	mysql_close($vLink);	
?>